package com.letoken.platform.repo.mall.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.util.LocalEnvUtils;
import com.letoken.platform.repo.app.req.mall.QueryMallOrderHistoryReq;
import com.letoken.platform.repo.app.req.mall.ShopOrderHistoryReq;
import com.letoken.platform.repo.app.res.mall.*;
import com.letoken.platform.repo.ecommerce.po.EcommerceIssue;
import com.letoken.platform.repo.mall.po.AppMallOrderInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.letoken.platform.repo.mall.req.InitOldData;
import com.letoken.platform.repo.mall.req.MergeData;
import io.swagger.annotations.ApiModelProperty;
import nonapi.io.github.classgraph.json.Id;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.checkerframework.checker.index.qual.SameLen;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 店铺订单，账单流水表，消费这和商家公用 Mapper 接口
 * </p>
 *
 * @author author
 * @since 2023-04-23
 */
public interface AppMallOrderInfoMapper extends BaseMapper<AppMallOrderInfo> {

    @Select("select o.user_id,o.id,o.create_time,o.amount,u.head_portrait,u.user_name,o.contributing,m.mall_name,o.update_time,o.seq_no,o.pay_type,c.level as oneCategoryName,o.amountCAP from app_mall_order_info o  inner join app_mall_info m on o.mall_id = m.id  inner join app_user  u on o.user_id = u.id left join app_mall_menu_config c on m.one_category = c.id where o.id = #{id} and o.mall_id = #{mallId} ")
    MallOrderDetailRes shopOrderDetail(@Param("id") Integer id ,@Param("mallId") Integer mallId);

    Page<MallOrdersRes> shopOrders(@Param("page") Page<MallOrdersRes> page ,@Param("req")  ShopOrderHistoryReq req);


    Page<MallUserOrdersRes> userOrders(@Param("page") Page<MallUserOrdersRes> page ,@Param("req")  ShopOrderHistoryReq req);




    @Select("select o.id,o.contributing,m.id as mallId, m.logo,m.mall_name,o.amount, o.create_time,o.update_time,o.seq_no ,c.level as oneCategoryName,o.pay_type,o.state,o.amountCAP  from app_mall_order_info o inner join  app_mall_info m on o.mall_id = m.id left join app_mall_menu_config c on m.one_category = c.id  where o.id = #{id} and o.user_id = #{userId} ")
    MallUserOrderDetailRes userOrderDetail(@Param("id") Integer id ,@Param("userId") Integer userId);


    Page<QueryMallOrderHistoryRes> orderList(@Param("page") Page<QueryMallOrderHistoryRes> page , @Param("req") QueryMallOrderHistoryReq req);


    @Select("select IFNULL(sum(amount),0) from app_mall_order_info where mall_id = #{mallId} and user_id = #{userId} and type = #{type} and DATE_FORMAT(create_time, '%Y-%m') = #{month}")
    BigDecimal queryTotalByType(@Param("mallId") Integer mallId,
                                @Param("userId") Integer userId,
                                @Param("type") Integer type,
                                @Param("month") String month);


    @Select("select IFNULL(sum(amount),0) as turnover,IFNULL(count(1),0) as orderNum  from app_mall_order_info where mall_id = #{mallId}")
    Map<String,BigDecimal> totalOrderNum(@Param("mallId") Integer mallId);



    @Select("select 1 from app_mall_order_info where id = #{orderId} limit 1")
    Integer orderExists(@Param("orderId") Integer orderId);



    @Select("select contributing ,sum(amount) as amount from app_mall_order_info where create_time >= #{startTime} and create_time < #{endTime} group by contributing ")
    List<MergeData> dayBonus(@Param("startTime")LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    @Select("select 1 from app_mall_ambassador_record where create_time >= #{startTime} and create_time < #{endTime} and mall_type = 1  limit 1 ")
    Integer existsBonus(@Param("startTime")LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);




    @Select("select id,mall_id,user_id from app_mall_order_info where create_time <= #{endTime} and state = 0  ")
    List<AppMallOrderInfo> waitEvaluate(@Param("endTime") LocalDateTime endTime);


    void updateBatchStateById(@Param("list") List<AppMallOrderInfo> list);


    @Select("select mall_id as mallId, DATE_FORMAT(create_time, '%Y-%m-%d') as date,sum(1) as orderNum,sum(amount) as turnover,sum(amount*contributing) as contribut from app_mall_order_info where create_time < #{endTime} group by DATE_FORMAT(create_time, '%Y-%m-%d'),mall_id")
    List<InitOldData> groupByDate(@Param("endTime")LocalDateTime endTime);


    @Select(" select o.user_id userId,i.user_id mallUserId,o.amount orderAmount,o.contributing contributing,o.id orderId from app_mall_order_info o,app_mall_info i where o.mall_id= i.id and   o.create_time>='2024-01-26 12:00:00' and o.create_time<='2024-02-24 23:59:59'")
    List<EcommerceIssue>  getEcommerceIssue();
}
